//
//  ZZCForgetPasswordThreeController.m
//  ZZC-Finance
//
//  Created by mac on 2017/5/21.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCForgetPasswordThreeController.h"
#import "ZZCLoginController.h"

@interface ZZCForgetPasswordThreeController ()

@end

@implementation ZZCForgetPasswordThreeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"忘记密码";
    [self.passwordTextField  becomeFirstResponder];
    [self.passwordTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    self.passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.passwordTextField.secureTextEntry = YES;

}

- (IBAction)confirmClick {
    //跳回登录界面
    [self submitPassword];
}

- (void)submitPassword {
//    if (self.passwordTextField.text == nil) {
//        [SVProgressHUD showInfoWithStatus:@"请设置密码"];
//        return;
//    }
    //    判断密码
    if (![ZZCUtils checkPassword:self.passwordTextField.text]) {
        NSLog(@"密码格式错误");
        [SVProgressHUD showInfoWithStatus:@"密码只能由字母数字下划线组成(6-16位)"];
        return;
    }

    NSDictionary *dict = @{@"phone":self.phone,
                           @"password":self.passwordTextField.text,
                           @"code":self.code
                           };
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:sign];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],
                            @"sign":sign,
                            @"phone":self.phone,
                            @"password":self.passwordTextField.text,
                            @"code":self.code
                            };
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_FORGETPASSWORD parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            ZZCLog(@"-%@",responseObject);
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
            if ([responseObject[@"code"] isEqual:@(200)]) {
                //跳回登录界面
                for (UIViewController *controller in self.navigationController.viewControllers) {
                    if ([controller isKindOfClass:[ZZCLoginController class]]) {
                        [self.navigationController popToViewController:controller animated:YES];
                        //这里可以做回到登录界面的一些数据操作
                    }
                }

            }
        }
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (void)textFieldDidChange:(UITextField *)textField{
    UITextRange *selectedRange = textField.markedTextRange;
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    if (!position) {
        // 没有高亮选择的字
        // 1. 过滤非汉字、字母、数字字符
        self.passwordTextField.text = [self filterCharactor:textField.text withRegex:@"[^a-zA-Z0-9\u4e00-\u9fa5]"];
        // 2. 截取
        if (self.passwordTextField.text.length >= 11) {
            self.passwordTextField.text = [self.passwordTextField.text substringToIndex:11];
        }
    } else {
        // 有高亮选择的字 不做任何操作
    }
}
// 过滤字符串中的非汉字、字母、数字
- (NSString *)filterCharactor:(NSString *)string withRegex:(NSString *)regexStr{
    NSString *filterText = string;
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:&error];
    NSString *result = [regex stringByReplacingMatchesInString:filterText options:NSMatchingReportCompletion range:NSMakeRange(0, filterText.length) withTemplate:@""];
    return result;
}
// return键按下
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    if (textField.text.length < 2) {
        // 提示用户，名称长度至少为2位
        return NO;
    }
    // 设置用户名
    return YES;
}

@end
